package algorithm

import (
	"fmt"
	"path/filepath"
	"time"

	"b2c-delivery-optimization/internal/model"

	"github.com/wcharczuk/go-chart/v2/drawing"
)

// WaveAllocationVisualizer 波次分配可视化器
type WaveAllocationVisualizer struct {
	renderer *ChartRenderer
	colors   []drawing.Color
}

// NewWaveAllocationVisualizer 创建波次分配可视化器
func NewWaveAllocationVisualizer(outputDir string) *WaveAllocationVisualizer {
	return &WaveAllocationVisualizer{
		renderer: NewChartRenderer(outputDir),
		colors: []drawing.Color{
			drawing.Color{R: 0, G: 128, B: 255, A: 255}, // 蓝色
			drawing.Color{R: 0, G: 255, B: 0, A: 255},   // 绿色
			drawing.Color{R: 255, G: 165, B: 0, A: 255}, // 橙色
			drawing.Color{R: 255, G: 0, B: 0, A: 255},   // 红色
			drawing.Color{R: 128, G: 0, B: 128, A: 255}, // 紫色
		},
	}
}

// GenerateWaveDistributionChart 生成波次分布图表
func (v *WaveAllocationVisualizer) GenerateWaveDistributionChart(waveOrders [][]model.Order) error {
	// 准备数据
	labels := make([]string, len(waveOrders))
	values := make([]float64, len(waveOrders))
	for i, orders := range waveOrders {
		labels[i] = fmt.Sprintf("Wave %d", i+1)
		values[i] = float64(len(orders))
	}

	// 生成图表
	return v.renderer.RenderBarChart("波次订单分布", "波次", "订单数量", labels, values, filepath.Join(v.renderer.outputDir, "wave_distribution.png"))
}

// GeneratePriorityDistributionChart 生成优先级分布图表
func (v *WaveAllocationVisualizer) GeneratePriorityDistributionChart(waveOrders [][]model.Order) error {
	// 准备数据
	priorityMap := make(map[int]int)
	for _, wave := range waveOrders {
		for _, order := range wave {
			priorityMap[order.Priority]++
		}
	}

	labels := make([]string, len(priorityMap))
	values := make([]float64, len(priorityMap))
	i := 0
	for priority, count := range priorityMap {
		labels[i] = fmt.Sprintf("P%d", priority)
		values[i] = float64(count)
		i++
	}

	// 生成图表
	return v.renderer.RenderBarChart("波次优先级分布", "波次-优先级", "订单数量", labels, values, filepath.Join(v.renderer.outputDir, "priority_distribution.png"))
}

// GenerateLocationDistributionChart 生成位置分布图表
func (v *WaveAllocationVisualizer) GenerateLocationDistributionChart(waveOrders [][]model.Order) error {
	// 准备数据
	var xValues, yValues []float64
	for _, wave := range waveOrders {
		for _, order := range wave {
			xValues = append(xValues, order.Location.Longitude)
			yValues = append(yValues, order.Location.Latitude)
		}
	}

	// 生成图表
	return v.renderer.RenderScatterChart("订单位置分布", "经度", "纬度", xValues, yValues, filepath.Join(v.renderer.outputDir, "location_distribution.png"))
}

// GenerateAlgorithmMetricsChart 生成算法指标图表
func (v *WaveAllocationVisualizer) GenerateAlgorithmMetricsChart(metrics []AlgorithmMetrics) error {
	// 准备数据
	times := make([]time.Time, len(metrics))
	seriesData := make([][]float64, 3) // 3个指标：优先级得分、位置得分、平衡得分
	seriesNames := []string{"优先级得分", "位置得分", "平衡得分"}

	// 初始化每个指标的切片
	for i := range seriesData {
		seriesData[i] = make([]float64, len(metrics))
	}

	for i, metric := range metrics {
		times[i] = metric.Timestamp
		seriesData[0][i] = metric.PriorityScore
		seriesData[1][i] = metric.LocationScore
		seriesData[2][i] = metric.BalanceScore
	}

	// 生成图表
	return v.renderer.RenderTimeSeriesChart("算法指标趋势", "迭代次数", "指标值", times, seriesData, seriesNames, filepath.Join(v.renderer.outputDir, "algorithm_metrics.png"))
}

// AlgorithmMetrics 算法指标
type AlgorithmMetrics struct {
	Timestamp     time.Time
	PriorityScore float64
	LocationScore float64
	BalanceScore  float64
}
